#include "backdrop_text_fx.h"
#include "ui/ui_widget.h"
#include "font/font.h"

#include "lang/lang.h"
#include "util/timer.h"				// log
#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    

using namespace backdrop;

BackdropTextFX::BackdropTextFX(camera::AbstractCamera* pCam, BackdropSFX* pParent, const std::string& Image, const std::string& Font, float Time, int	MaxCount, bool Floating, bool AutoResize ) :
	BackgdropSFXSet(pCam, pParent),
	m_MaxCount(0),
	m_CurrentCount(0),
	m_TextA(NULL),
	m_Floating(Floating),
	m_AutoResize(AutoResize)
{
	m_MaxCount = MaxCount;
	m_TextA = new TextInst[m_MaxCount];
	for (int i=0; i<m_MaxCount; i++)
	{
		m_TextA[i].pWidget = new ui::UIWidget(NULL, "TestFX");
		m_TextA[i].pWidget->SetOffImage(Image);
		m_TextA[i].pWidget->SetFont(Font);
		m_TextA[i].Active = false;
		m_TextA[i].Life.SetLimit(Time);
	}
}

BackdropTextFX::~BackdropTextFX()
{
	for (int i=0; i<m_MaxCount; i++)
	{
		UTIL_SAFE_DELETE(m_TextA[i].pWidget);
	}
	UTIL_SAFE_DELETEA(m_TextA);
}

void					BackdropTextFX::Render()
{
	for (int i=0; i<m_MaxCount; i++)
	{
		if (m_TextA[i].Active && m_TextA[i].pWidget)	
		{
			m_TextA[i].pWidget->Render(true);
		}
	}
}

void					BackdropTextFX::Tick(float dt)
{
	const math::Vec2	FloatSpeed(0, -0.2f);
	for (int i=0; i<m_MaxCount; i++)
	{
		if (m_TextA[i].Active && m_TextA[i].pWidget)	
		{
			m_TextA[i].pWidget->Tick(dt);
			if (m_Floating)
			{
				m_TextA[i].pWidget->SetPosition(m_TextA[i].pWidget->GetPosition() + (FloatSpeed * dt));
			}
			m_TextA[i].Life.Tick(dt);
			m_TextA[i].pWidget->SetColor(1, 1, 1, 1.0f-m_TextA[i].Life.GetProgress());
			m_TextA[i].pWidget->SetTextColor(1, 1, 1, 1.0f-m_TextA[i].Life.GetProgress());
			if (m_TextA[i].Life.IsReady())
			{
				m_TextA[i].Active = false;
				m_CurrentCount--;
			}
		}
	}
}

void					BackdropTextFX::AddText(const std::wstring& Text, const math::Vec2& Pos, const math::Vec2& Size, COLOR Color, bool Centered)
{
	if (m_CurrentCount == m_MaxCount) return;	// cannot

	for (int i=0; i<m_MaxCount; i++)
	{
		if (!m_TextA[i].Active && m_TextA[i].pWidget)
		{
			m_TextA[i].Active = true;
			m_TextA[i].pWidget->SetPosition(Pos);
			if (m_AutoResize)
			{
				//m_TextA[i].pWidget->RecalcSize();
				Font* pFont = m_TextA[i].pWidget->GetTextObj()->GetFont();
				if (pFont)
				{
					math::Vec2	From, TextSize;
					const math::Vec2 Border(0.05f, 0.02f);
					pFont->GetTextExtent(Text, Size, From, TextSize);
					m_TextA[i].pWidget->SetSize(TextSize + Border*3);

					if (Centered)
					{
						m_TextA[i].pWidget->SetPosition(Pos - (m_TextA[i].pWidget->GetSize()*0.5f));
					}
				}
			}
			else
			{
				m_TextA[i].pWidget->SetSize(Size);
			}
			m_TextA[i].pWidget->SetTextW(Text);
			m_TextA[i].Life.Reset();
			m_CurrentCount++;
			return;
		}
	}
}

void					BackdropTextFX::AddTextA(const std::string& Text, const math::Vec2& Pos, const math::Vec2& Size, COLOR Color, bool Centered)
{
	AddText(lang::Lang::ConvertAtoW(Text), Pos, Size, Color, Centered);
}
